//
//  M4HomeFirstCell.swift
//  M4
//
//  Created by YuChuang on 2024/8/15.
//

import UIKit

enum M4HomeStatusAction: Int {
    case M4HomeFirstAction = 0
    case M4HomeDaysAction = 1
    case M4HomeSecondAction = 2
    case M4HomeSectionTimeEndAction = 3
}

class M4HomeFirstCell: M4BaseTableViewCell {
    
    @IBOutlet weak var M4HomeFirstBGView: UIView!
    
    @IBOutlet weak var M4HomeFirstMax: UILabel!
    
    @IBOutlet weak var M4HomeFirstDayBtn1: UIButton!
    
    @IBOutlet weak var M4HomeFirstDayBtn2: UIButton!
    
    @IBOutlet weak var M4HomeFirstDayBtn3: UIButton!
    
    @IBOutlet weak var tipBgView: UIView!
    
    var gradientLayer: CAGradientLayer?
    var M4HomeFirstToPageBlock: (() -> Void)?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        M4HomeFirstDayBtn1.isSelected = true
        M4HomeFirstDayBtn1.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .bold)
    }
    
    override func M4UpdateDataAndUI() {
        let data = data as? M4HomeProductModel
        M4HomeFirstMax.text = M4String.M4BlankAmount(original: "\(data?.AeJZUX ?? 0)")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if gradientLayer == nil {
                let bgLayer1 = CAGradientLayer()
                bgLayer1.colors = [
                    UIColor(red: 255/255, green: 246/255, blue: 246/255, alpha: 1).cgColor,
                    UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1).cgColor
                ]
                bgLayer1.locations = [0, 1]
                bgLayer1.startPoint = CGPoint(x: 0, y: 0.44)
                bgLayer1.endPoint = CGPoint(x: 0.46, y: 0.46)
                bgLayer1.frame = tipBgView.bounds

            tipBgView.layer.insertSublayer(bgLayer1, at: 0)
                gradientLayer = bgLayer1
            } else {
                gradientLayer?.frame = tipBgView.bounds
            }
    }
    
    @IBAction func M4HomeDaysAction(_ sender: UIButton) {
        sender.isSelected = true
        if sender == M4HomeFirstDayBtn1 {
            M4HomeFirstDayBtn2.isSelected = false
            M4HomeFirstDayBtn3.isSelected = false
        }
        else if sender == M4HomeFirstDayBtn2 {
            M4HomeFirstDayBtn1.isSelected = false
            M4HomeFirstDayBtn3.isSelected = false
        }
        else {
            M4HomeFirstDayBtn1.isSelected = false
            M4HomeFirstDayBtn2.isSelected = false
        }
        M4HomeFirstDayBtn1.titleLabel?.font = M4HomeFirstDayBtn1.isSelected ? UIFont.systemFont(ofSize: 16, weight: .bold) : UIFont.systemFont(ofSize: 16, weight: .regular)
        M4HomeFirstDayBtn2.titleLabel?.font = M4HomeFirstDayBtn2.isSelected ? UIFont.systemFont(ofSize: 16, weight: .bold) : UIFont.systemFont(ofSize: 16, weight: .regular)
        M4HomeFirstDayBtn3.titleLabel?.font = M4HomeFirstDayBtn3.isSelected ? UIFont.systemFont(ofSize: 16, weight: .bold) : UIFont.systemFont(ofSize: 16, weight: .regular)
        sender.M4EventUnavailable = false
    }
    

    @IBAction func M4HomeFirstToPageClick(_ sender: UIButton) {
        M4CellActionDelegate?.M4CellAction(M4IDAction(actionType: M4HomeStatusAction.M4HomeFirstAction.rawValue))
        DispatchQueue.main.asyncAfter(deadline: .now()+2) {
            sender.M4EventUnavailable = false
        }
    }
    
    
    
}
